package com.ygqh.baby.service.wechat.model.good;

import com.foxinmy.weixin4j.exception.WeixinException;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

/**
 * 好物圈导入订单请求参数
 *
 * @Author: sunshuo
 * @Date: 2019/5/21 17:34
 * @Version: 1.0
 */
public class GoodImportOrderRequestParam {

	/**
	 * 订单信息集（单次请求订单数量不可超过10单）
	 */
	private List<GoodOrder> orderList;

	/**
	 * 行为类型（add-order 导入新订单，update-order 更新订单）
	 */
	private ImportOrderAction action;

	/**
	 * 是否为历史订单: 如果订单中的物品为首次导入，订单中的物品信息会该物品的初始化信息；如果为历史订单，请求URL的is_history参数值为1
	 */
	private Integer isHistory;

	public List<GoodOrder> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<GoodOrder> orderList) {
		this.orderList = orderList;
	}

	public ImportOrderAction getAction() {
		return action;
	}

	public void setAction(ImportOrderAction action) {
		this.action = action;
	}

	public int getIsHistory() {
		return isHistory;
	}

	public void setIsHistory(int isHistory) {
		this.isHistory = isHistory;
	}

	public void validateRequestParamForImportOrder()
			throws WeixinException {
		if (CollectionUtils.isEmpty(orderList)) {
			throw new WeixinException("订单信息集为空");
		}
		if (orderList.size() > 10) {
			throw new WeixinException("单次请求订单数量不可超过10单");
		}
		if (action == null) {
			throw new WeixinException("必须明确操作类型");
		}
		if (isHistory == null) {
			throw new WeixinException("必须明确是否为历史订单");
		}
	}

	public String getRequestUri() {
		return "https://api.weixin.qq.com/mall/importorder?action=%s&is_history=%s&access_token=%s";
	}

	public enum ImportOrderAction {

		ADD_ORDER("add-order"), UPDATE_ORDER("update-order");

		public String value;

		ImportOrderAction(String value) {
			this.value = value;
		}

		public String getValue() {
			return value;
		}
	}
}
